package com.itheima.config;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisTemplateConfig extends CachingConfigurerSupport {


    // 主动构建出来一个RedisTemplate对象，然后返回给spring管理它
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory){

        //1. 创建RedisTemplate对象
        RedisTemplate redisTemplate = new RedisTemplate();

        //2. 设置序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());

        //3. 设置连接工厂，好让RedisTemplate 知道要去操作哪个地方的redis
        redisTemplate.setConnectionFactory(connectionFactory);

        //4. 返回redis即可
        return redisTemplate;

    }

}
